﻿//Write a program that enters the coefficients a, b and c of a quadratic equation a*x^2 + b*x + c = 0 and calculates and prints its real roots. 
//Note that quadratic equations may have 0, 1 or 2 real roots.

using System;
using System.Globalization;
using System.Text;
using System.Threading;

class SolveQuadraticEquation
{
    static void Main()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
        Console.OutputEncoding = Encoding.UTF8;
        Console.WriteLine("Calculate quadratic equation a" + '\u00B2' + "+bx+c=0");
        Console.Write("a = ");
        string aString = Console.ReadLine().Replace(",", ".");
        Console.Write("b = ");
        string bString = Console.ReadLine().Replace(",", ".");
        Console.Write("c = ");
        string cString = Console.ReadLine().Replace(",", ".");
        double a;
        double b;
        double c;
        if (
            double.TryParse(aString, out a) &&
            double.TryParse(bString, out b) &&
            double.TryParse(cString, out c)
            )
        {
            if (a == 0)
            {
                Console.WriteLine("The equation is a linear: x" + '\u2081' + " = {0:0.000}", -c / b);
            }
            else if (b == 0)
            {
                Console.WriteLine("The equation roots are: x =" + '\u00b1' + "{0:0.000}", Math.Sqrt(-c / a));
            }
            else
            {
                double discriminant = b * b - 4 * a * c;
                if (discriminant > 0)
                {
                    double x1 = (-b + Math.Sqrt(discriminant)) / (2 * a);
                    double x2 = (-b - Math.Sqrt(discriminant)) / (2 * a);
                    Console.WriteLine("The equation roots are: x" + '\u2081' + " = {0:0.000}, x" + '\u2082' + " = {1:0.000}", x1, x2);
                }
                else if (discriminant == 0)
                {
                    Console.WriteLine("The equation have only one root: x" + '\u2081' + " = {0:0.000}", -(b / (2 * a)));
                }
                else
                {
                    Console.WriteLine("The equation doesn't have real roots.");
                }
            }
        }
        else
            Console.WriteLine("Input error ! a, b and c accept only numeric values.");
    }
}